package com.health.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.health.dao.SetMealMapper;
import com.health.pojo.Setmeal;
import com.health.vo.PageResult;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SetMealServiceImpl implements SetMealService {

    @Resource
    private SetMealMapper setMealMapper;

    @Override
    public PageResult<Setmeal> findPage(Integer pageNow, Integer pageSize, String query) {
        //分页插件
        PageHelper.startPage(pageNow, pageSize);
        //执行查询，Page中包含了查询到的总数，以及分页后的列表
        Page<Setmeal> page = setMealMapper.findByQuery(query);

        //封装查询到的总数与列表
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteById(Integer id) {
        //到关联表中查询该套餐是否有被某个订单使用
        int count = setMealMapper.findCountBySetMealId(id);
        if (count > 0) {
            //已被使用，抛出异常，取消删除操作
            throw new RuntimeException("有订单正在使用该套餐，无法删除！");
        }

        //未被使用则先删除套餐与检查组的关联
        setMealMapper.deleteSetMealCheckGroup(id);

        //再删除套餐
        return setMealMapper.deleteById(id);
    }

    @Override
    public int add(Setmeal setmeal, Integer[] checkGroupIds) {
        //新增套餐
        int count = setMealMapper.add(setmeal);

        //新增套餐与检查组的关联关系
        if (checkGroupIds != null && checkGroupIds.length > 0) {
            //获取新增的套餐id
            Integer seatmealId = setmeal.getId();
            setMealMapper.addSetMealCheckGroup(seatmealId, checkGroupIds);
        }

        return count;
    }

    @Override
    public Setmeal findById(Integer id) {
        return setMealMapper.findById(id);
    }

    @Override
    public List<Integer> findCheckGroupIds(Integer id) {
        return setMealMapper.findCheckGroupIds(id);
    }

    @Override
    public int update(Setmeal setmeal, Integer[] checkGroupIds) {
        //更新套餐信息
        int count = setMealMapper.update(setmeal);

        //删除套餐与检查组的关联信息
        Integer setmealId = setmeal.getId();
        setMealMapper.deleteSetMealCheckGroup(setmealId);
        //添加新关系
        if (checkGroupIds != null && checkGroupIds.length > 0) {
            setMealMapper.addSetMealCheckGroup(setmealId, checkGroupIds);
        }

        return count;
    }

    @Override
    public List<String> findImgs() {
        return setMealMapper.findImgs();
    }

    @Override
    public List<Setmeal> findAll() {
        return setMealMapper.findAll();
    }

    @Override
    public Setmeal findDetail(Integer id) {
        return setMealMapper.findDetail(id);
    }

    @Override
    public List<Map<String, Object>> findSetMealReport() {
        return setMealMapper.findSetMealReport();
    }

    @Override
    public List<Map<String, Object>> findSetMealCount(Date start, Date end) {
        return setMealMapper.findSetMealCount(start, end);
    }

    @Override
    public List<Setmeal> findByQuery(String query) {
        return setMealMapper.findByQueryNoPage(query);
    }
}
